package com.qb.ems.controller;

import com.qb.ems.entity.User;
import com.qb.ems.service.UserService;
import org.apache.shardingsphere.sharding.merge.dql.ShardingDQLResultMerger;
import org.hibernate.tool.schema.internal.AbstractSchemaMigrator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Controller
public class UserController {

    private static  int id =0;
    @Autowired
    private UserService userService;

    @RequestMapping("/save")
    @ResponseBody
    public String saveUser() {
        id++;
        User user = new User();
        user.setCardNum(id);
        user.setName("t");
        user.setPassword("123457");
        user.setEmail("testuser@gbasedbt.com");
        user.setTime(new Date());
        user.setB(true);
        user.setId(id);
        user.setId2(id);
        userService.addUser(user);
        return "保存用户成功";
    }

    @RequestMapping("/find/{id}")
    @ResponseBody
    public Optional<User> findUser(@PathVariable Integer id) {

        return userService.findUserById(id);
    }

    @RequestMapping("/findall/{id}")
    @ResponseBody
    public List<User> findAllUser(@PathVariable Integer id) {
        return userService.findAllUserById(id);
    }

    @RequestMapping("/del/{id}")
    @ResponseBody
    public String delUser(@PathVariable Integer id) {
        userService.delUser(id);
        return "删除用户成功";
    }
    @RequestMapping("/page/{page}/{size}")
    @ResponseBody
    public Page<User> findUserByPage(@PathVariable Integer page, @PathVariable Integer size){
        return userService.getUserWithPage(page, size);
    }
}
